import { AstContext } from "../../context";
import { AstVisitor } from "../../visitor";
import { parseFollowSelectorLayoutRoot } from "./utils/parseFollowSelectorLayoutRoot";
import { R } from './R'
import { Pages } from './Pages'
import { parseBottomBarAndGreyCoverContainer } from "./utils/parseBottomBarAndGreyCoverContainer";
import { parseChildren } from "./utils/parseChildren";
import { parseObject } from "./utils/parseObject";
import { parseBlockTopAdjustView } from "./utils/parseBlockTopAdjustView";
import { parseSwipe } from "./utils/parseSwipe";
import { parseTopBar } from "./utils/parseTopBar";
import { parseSlideMenu } from "./utils/parseSlideMenu";
import { parseHomeFragmentContainer } from "./utils/parseHomeFragmentContainer";
import { currentPage, moneys } from "./utils/consts";
import { parseLiveSlideContainer } from "./utils/parseLiveSlideContainer";
import { parseLiveBottomBarLeftLayout } from "./utils/parseLiveBottomBarLeftLayout";

type Node = AutoJs.UiObject
export class PageVisitor extends AstVisitor {
    visitImageButton(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.left_btn:
                ctx.set('back', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            default:
                return false;
        }
    }
    visitViewPager(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return true;
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.live_slide_square_side_bar_layout:
                ctx.set('currentPage', Pages.liveSlide)
                return false;
            case R.live_red_packet_container_view_pager:
                ctx.set(`currentPage`, Pages.redPacket)
                return parseChildren(this, ast, ctx, 'redsList')
            default:
                return false;
        }
    }
    private _parsesRewarding(ast: Node, ctx: AstContext) {
        parseObject(this, ast, ctx, 'rewarding')
    }
    private _parseRewardResult(ast: Node, ctx: AstContext) {
        ctx.set('currentPage', Pages.redPacketResult)
        parseObject(this, ast, ctx, 'rewardResult')
    }
    private _parseReward(ast: Node, ctx: AstContext) {
        parseObject(this, ast, ctx, 'reward')
    }
    private _parseRewardGrab(ast: Node, ctx: AstContext) {
        parseObject(this, ast, ctx, 'grabs')
        return true;
    }
    private _parseMultiChat(ast: Node, ctx: AstContext) {
        parseChildren(this, ast, ctx, 'chats')
    }
    visitFrameLayout(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.kwai_rn_fragment_root:
                const textView = ctx.bfsOne(ast, it => it.visibleToUser() && (it.className() === `android.widget.TextView` || it.className() === `android.widget.ImageView`))
                if (textView) ctx.set(currentPage, Pages.kwaiRn)
                return false;
            case R.home_fragment_container:
                return parseHomeFragmentContainer(this, ast, ctx)
            case R.block_top_adjust_view:
                return parseBlockTopAdjustView(this, ast, ctx)
            case R.bottom_bar_and_grey_cover_container:
                return parseBottomBarAndGreyCoverContainer(this, ast, ctx)
            case R.follow_selector_layout_root:
                // 推荐排序|时间排序|只看朋友 关注下拉选择框
                return parseFollowSelectorLayoutRoot(this, ast, ctx)
            case R.share_panel:
                ctx.set('currentPage', Pages.share)
                return false;
            case R.left_btn:
                ctx.set('toSlideMenu', () => {
                    ast.click()
                    ast.recycle()
                })
                return false;
            case R.live_square_side_bar_layout_close_view:
                ctx.set('closeSlide', () => {
                    ast.click()
                    ast.recycle()
                })
                return false;
            case R.scroll_page_container:
                this._parsesRewarding(ast, ctx)
                return true;
            case R.live_activity_red_packet_result_gift_reward_container:
            case R.result_page_container:
                if (ast.childCount() > 0) {
                    this._parseRewardResult(ast, ctx)
                }
                return true;
            case R.prepare_reward_info_view:
                this._parseReward(ast, ctx)
                return true;
            case R.live_activity_red_packet_grab_reward_container:
                this._parseRewardGrab(ast, ctx)
                return true;
            case R.live_multi_chat_render_ui_layout:
                this._parseMultiChat(ast, ctx)
                return true;
            case R.live_bottom_bar_gift_container:
                if (!ast.visibleToUser()) return true;
                ctx.set('toGift', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.webView:
                ctx.set(`currentPage`, Pages.webview)
                return false;
            case R.live_bottom_dialog_container_root:
                ctx.set(`currentPage`, Pages.liveBottomDialog)
                return false;
            case R.live_play_root_container:
            case R.live_slide_container:
                return parseLiveSlideContainer(this, ast, ctx)
            case R.close_fragment_container:
                if (ast.childCount() > 0) {
                    ctx.set(`currentPage`, Pages.liveClose)
                }
                return false;
            case R.live_merchant_bottom_bar_shop_container:
                ctx.set(`toShop`, () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.aggregate_container_layout:
                const visibleToUser = ast.visibleToUser()
                if (visibleToUser) ctx.set('currentPage', Pages.homeFollow)
                return false;
            case R.empty:
                if (!ctx.isTop()) {
                    const desc = ast.desc()
                    if (desc) {
                        ctx.set(`desc`, desc)
                    }
                    const clickable = ast.clickable()
                    if (clickable) {
                        ctx.set(`click`, () => {
                            ast.click();
                            ast.recycle()
                        })
                    }
                }
                return false;
            case R.swipe:
                return parseSwipe(this, ast, ctx)
            default:
                return false;
        }
    }
    visitHorizontalScrollView(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast);
        switch (sid) {
            case R.tab_layout:
                return parseBottomBarAndGreyCoverContainer(this, ast, ctx)
            case R.kcube_tab_strip:
                return parseTopBar(this, ast, ctx)
            case R.tabs:
                const tabContainer = ast.child(0)
                if (tabContainer) {
                    ctx.set('toSlideTab', (index: number) => {
                        const c = tabContainer.child(index)
                        if (c) {
                            c.click()
                            c.recycle()
                        }
                    })
                    ctx.eachChild(tabContainer, (it, index) => {
                        const selected = it.selected()
                        const text = it.text()
                        if (selected && text) {
                            ctx.set('slideTab', text)
                            ctx.set('slideTabIndex', index)
                        }
                    })
                }
                return true;
            default:
                return false;
        }
    }

    visitSlidingPaneLayout(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.follow_selector_layout_root:
                // 推荐排序|时间排序|只看朋友 关注下拉选择框
                return parseFollowSelectorLayoutRoot(this, ast, ctx)
            case R.empty:
                const childCount = ast.childCount()
                if (childCount === 1) {
                    // 侧边栏打开了
                    ctx.set(`currentPage`, Pages.menu)
                    parseSlideMenu(this, ast, ctx)
                    return true;
                } else {
                    const child = ast.child(1)
                    child && this.visit(child, ctx)
                }
                return true;
            default:
                return false;
        }
    }
    private _parseOtherMenus(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        parseChildren(this, ast, ctx, 'others')
    }
    private _parseTopMenus(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        parseChildren(this, ast, ctx, 'tops')
    }
    private _parseCommonMenus(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        parseChildren(this, ast, ctx, 'commons')
    }
    private _parseItemList(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        const items: any[] = []
        ctx.set('listNode', ast)
        ctx.eachChild(ast, it => {
            if (it.clickable()) {
                const c = ctx.create()
                c.set('click', () => {
                    it.click()
                    it.recycle()
                })
                this.visit(it, c)
                const obj = c.all()
                if (obj && Object.keys(obj).length > 0) {
                    items.push(obj)
                }
            }
        })
        ctx.set('list', items)
    }
    private _parseLiveBottomBar(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        ctx.set(currentPage, Pages.liveBottomDialog)
        const items: any[] = []
        ctx.set('listNode', ast)
        ctx.eachChild(ast, it => {
            if (it.clickable()) {
                const c = ctx.create()
                c.set('click', () => {
                    it.click()
                    it.recycle()
                })
                this.visit(it, c)
                const obj = c.all()
                if (obj && Object.keys(obj).length > 0) {
                    items.push(obj)
                }
            }
        })
        ctx.set('list', items)
    }
    visitRecyclerView(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.live_bottom_bar_recycle_view:
                this._parseLiveBottomBar(ast, ctx)
                return true;
            case R.item_list:
                this._parseItemList(ast, ctx)
                return true;
            case R.top_menus_recycler_view:
                this._parseTopMenus(ast, ctx)
                return true;
            case R.other_menus_recycler_view:
                this._parseOtherMenus(ast, ctx)
                return true;
            case R.common_menus_recycler_view:
                this._parseCommonMenus(ast, ctx)
                return true;
            case R.sub_tab_view:
                ctx.set(`toSlideSubTab`, (index: number) => {
                    const c = ast.child(index)
                    if (c) {
                        c.click()
                        c.recycle()
                    }
                })
                ctx.eachChild(ast, (it, index) => {
                    const selected = it.selected()
                    const text = it.text()
                    if (selected && text) {
                        ctx.set('slideSubTab', text)
                        ctx.set('slideSubTabIndex', index)
                    }
                })
                return true;
            case R.live_square_side_bar_fragment_layout_recycler_view:
                const slideList: any[] = []
                ctx.eachChild(ast, it => {
                    if (it.className() === `android.widget.LinearLayout`) {
                        const c = ctx.create()
                        this.visit(it, c)
                        const obj = c.all()
                        if (obj && Object.keys(obj).length > 0) {
                            slideList.push(obj)
                        }
                    }
                })
                ctx.set(`slideList`, slideList)
                return true;
            case R.live_play_closed_v2_feed_recycler_view:
                if (!ast.visibleToUser()) return true;
                ctx.set('listNode', ast)
                const livesList2: any[] = [];
                ctx.eachChild(ast, (it) => {
                    if (!it.visibleToUser()) return;
                    const c = ctx.create()
                    this.visit(it, c)
                    const obj = c.all()
                    if (obj && Object.keys(obj).length > 0) {
                        livesList2.push(obj)
                    }
                })
                ctx.set(`livesList`, livesList2)
                return true;
            case R.lucky_user_recycler_view:
                const luckyUserList: any[] = []
                ctx.eachChild(ast, it => {
                    const c = ctx.create()
                    this.visit(it, c)
                    const obj = c.all()
                    if (obj && Object.keys(obj).length > 0) {
                        luckyUserList.push(obj)
                    }
                })
                ctx.set(`luckyUserList`, luckyUserList)
                return true;
            case R.live_top_temp_play_right_rv:
                const activities: any[] = []
                ctx.eachChild(ast, it => {
                    const c = ctx.create()
                    this.visit(it, c)
                    const obj = c.all()
                    if (obj && Object.keys(obj).length > 0) {
                        activities.push(obj)
                    }
                })
                ctx.set(`activities`, activities)
                return true;
            case R.nearby_function_list:
                if (!ast.visibleToUser()) return true
                ctx.set('toSecondTab', (index: number) => {
                    const node = ast.child(index)
                    if (node) {
                        node.click()
                        node.recycle()
                    }
                    ast.recycle()
                })
                return false;
            case R.pymi_users_list:
                const userList: any[] = []
                ctx.filterChildren(ast, node => {
                    const cctx = ctx.create()
                    this.visit(node, cctx)
                    const user = cctx.all()
                    userList.push(user)
                    return true;
                })
                ctx.set(sid, userList)
                return true;
            case R.recycler_view:
                /**
                 * 第一个是 header
                 * 其余的是主播信息
                 */
                const parent = ast.parent()
                const psid = ctx.getSid(parent)
                if (parent && psid) {
                    if (psid === R.refresh_layout) {
                        if (!ast.visibleToUser()) return;
                        ctx.set('listNode', ast)
                        const livesList: any[] = [];
                        ctx.eachChild(ast, it => {
                            const sid = ctx.getSid(it)
                            if (sid) return;
                            if (it.clickable()) {
                                const cctx = ctx.create()
                                cctx.set(`redCard`, it)
                                this.visit(it, cctx)
                                const obj = cctx.all()
                                if (obj && Object.keys(obj).length > 0) {
                                    livesList.push(cctx.all())
                                }
                            }
                            return false;
                        })
                        ctx.set(`livesList`, livesList)
                        return true;
                    }
                    if (psid === R.coordinator_layout_sub_entrance) {
                        if (!ast.visibleToUser()) return;
                        ctx.set(`listNode`, ast)
                        const livesList: any[] = [];
                        ctx.eachChild(ast, it => {
                            if (it.clickable()) {
                                const cctx = ctx.create()
                                this.visit(it, cctx)
                                const obj = cctx.all()
                                if (obj && Object.keys(obj).length > 0) {
                                    livesList.push(obj)
                                }
                            }
                            return false;
                        })
                        ctx.set(`livesList`, livesList)
                        return true;
                    }
                }
                return false;
            case R.live_explore_fragment_recycler_view:
                if (!ast.visibleToUser()) return true;
                ctx.set(`listNode`, ast)
                const livesList: any[] = []
                ctx.filterChildren(ast, it => {
                    const className = it.className()
                    if (className === `android.widget.RelativeLayout`) {
                        const cctx = ctx.create()
                        this.visit(it, cctx)
                        livesList.push(cctx.all())
                    }
                    return true;
                })
                ctx.set(`livesList`, livesList)
                return true;
            case R.empty:
                return false;
            default:
                return false;
        }
    }
    /**
     * 这个会有很多哦
     */
    private _praseSettings(ast: Node, ctx: AstContext) {
        const settings: any[] = [];
        ctx.eachChild(ast, it => {
            if (it.clickable()) {
                const c = ctx.create()
                c.set('click', () => {
                    it.click()
                    it.recycle()
                })
                this.visit(it, c)
                settings.push(c.all())
            }
        })
        ctx.set(`settings`, settings)
    }
    visitViewGroup(ast: Node, ctx: AstContext) {
        if (!ast.visibleToUser()) return;
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.settings_layout:
                this._praseSettings(ast, ctx)
                return true;
            case R.start_live_layout:
                ctx.set(`startLive`, () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.live_play_closed_v2_container:
                const parent = ctx.parent;
                if (parent) {
                    parent.set('toMore', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return true;
            case R.live_audience_bottom_bar_follow:
                ctx.set('toFollow', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            default:
                return false;
        }
    }
    visitView(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.app:
                const webView = ctx.findInParent(ast, it => ctx.getSid(it) === 'webView')
                if (!webView) return false;
                const child = ast.child(0)?.child(0)
                if (child) {
                    const reds: any[] = []
                    ctx.eachChild(child, it => {
                        const c = ctx.create()
                        c.set('join', () => {
                            it.click()
                            it.recycle()
                        })
                        const time = it.child(0)?.text()
                        if (time) {
                            c.set('time', parseInt(time))
                        }
                        const desc = it.child(1)?.child(1)?.text()
                        if (desc) {
                            c.set('desc', desc)
                        }
                        const obj = c.all()
                        if (obj && Object.keys(obj).length > 0) {
                            reds.push(obj)
                        }
                    })
                    ctx.set('reds', reds)
                }
                return true;
            default:
                return false;
        }
    }
    visitImageView(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.pendant_close_area:
            case R.close_btn:
            case R.dialog_close:
            case R.image_view_mtpp_close_button:
                if (ast.clickable()) {
                    ctx.set('close', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return false;
            case R.live_square_side_bar_item_cover_view:
                if (!ctx.isTop()) {
                    ctx.set('click', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return true;
            case R.live_close:
            case R.live_red_packet_container_close_view:
                ctx.set('close', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.live_cover_icon_left_image:
                if (!ctx.isTop()) {
                    const bounds = ast.bounds()
                    ctx.set('bounds', bounds)
                }
                return true;
            default:
                return false;
        }
    }
    visitTextSwitcher(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.live_comment_text_view:
                if (!ast.visibleToUser()) return true;
                ctx.set('toComment', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.empty:
                return false;
            default:
                return false;
        }
    }
    private _parseShareToLayout(ast: Node, ctx: AstContext) {
        const list = ctx.get<any[]>('list') || []
        const c = ctx.create()
        ctx.eachChild(ast, node => {
            this.visit(node, c)
        })
        list.push(c.all())
        ctx.set('list', list)
    }
    visitLinearLayout(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.live_purchase_panel_container:
                if (ast.visibleToUser()) {
                    ctx.set(currentPage, Pages.liveBottomDialog)
                }
                return false;
            case R.live_bottom_bar_left_layout:
                return parseLiveBottomBarLeftLayout(this, ast, ctx)
            case R.share_to_layout:
                this._parseShareToLayout(ast, ctx)
                return false;
            default:
                return false;
        }
    }
    visitRelativeLayout(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.live_explore_feed_card_layout:
                if (!ast.visibleToUser()) {
                    return;
                }
                if (!ctx.isTop()) {
                    ctx.set(`redCard`, ast)
                    ctx.set('click', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return false;
            case R.empty:
                const clickable = ast.clickable()
                if (clickable) {
                    ctx.set('click', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return false;
            default:
                return false;
        }
    }
    private _parseTimeSecond(text: string) {
        const res = text.match(/(.*?)后开奖/)
        if (res && res.length > 0) {
            const [, time] = res;
            if (time.endsWith('分钟')) {
                return parseInt(time.replace('分钟', '')) * 60
            }
            if (time.endsWith('秒')) {
                return parseInt(time.replace('秒', ''))
            }
            return parseInt(time)
        }
    }
    visitButton(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        switch (sid) {
            case R.clear_cache:
                ctx.set('clearCache', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.empty:
                return false;
            default:
                return false;
        }
    }
    visitTextView(ast: Node, ctx: AstContext) {
        const sid = ctx.getSid(ast)
        const text = ast.text() || ""
        switch (sid) {
            case R.retry_btn:
                if (ast.clickable()) {
                    ctx.set('close', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                break;
            case R.empty_desc_text_view:
                if (!ctx.isTop()) {
                    ctx.set('result', text)
                }
                return true;
            case R.prepare_reward_password_text_view:
                ctx.set('password', text)
                return true;
            case R.live_bottom_bar_item_title_text_view:
                ctx.set('title', text)
                return true;
            case R.positive:
                const positiveParent = ast.parent()
                if (positiveParent) {
                    if (text === '我知道了' && ctx.getSid(positiveParent) === 'child_lock_panel') {
                        ctx.set(`currentPage`, Pages.childLock)
                        ctx.set('close', () => {
                            ast.click()
                            ast.recycle()
                        })
                    }
                }
                return true;
            case R.splash_skip_text:
                ctx.set('currentPage', Pages.splash)
                ctx.set('close', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.item_title:
                ctx.set('title', text)
                return true;
            case R.item_status:
                ctx.set('status', text)
                return true;
            case R.title_tv:
                const parent = ast.parent()
                if (parent) {
                    const pid = ctx.getSid(parent)
                    if (pid === 'title_root') {
                        switch (text) {
                            case '设置':
                                ctx.set('currentPage', Pages.settings)
                                break;
                            case '存储空间':
                                ctx.set('currentPage', Pages.storageSpace)
                                break;
                            case "请完成安全验证":
                                ctx.set(currentPage, Pages.kwaiCaptcha)
                                break;
                        }
                        ctx.set('title', text)
                    }
                    if (pid === 'root') {

                    }
                }
                return true;
            case R.close_floatView_tv:
            case R.child_lock_tv:
            case R.setting_tv:
                ctx.set('title', text)
                return true;
            case R.title:
                ctx.set(`title`, text)
                const clickable = ctx.findInParent(ast, it => it.clickable())
                if (clickable) {
                    ctx.set('click', () => {
                        clickable.click()
                        clickable.recycle()
                    })
                }
                return true;
            case R.title_more:
                const titleMore = ctx.findInParent(ast, it => it.clickable())
                if (titleMore) {
                    ctx.set('toMore', () => {
                        titleMore.click()
                        titleMore.recycle()
                    })
                }
                return true;
            case R.live_square_side_bar_item_location_view:
                ctx.set('location', text)
                return true;
            case R.live_play_closed_anchor_attribute_first_text_view:
                const fansResult = text.match(/(.*?)粉丝/)
                if (fansResult && fansResult.length > 0) {
                    const [, fansCount] = fansResult;
                    ctx.set(`fansCount`, parseInt(fansCount))
                }
                return true;
            case R.live_play_closed_user_operate_follow_text_view:
                if (text === `关注主播` && ast.clickable()) {
                    if (!ast.visibleToUser()) return true;
                    ctx.set('toFollow', () => {
                        ast.click()
                        ast.recycle()
                    })
                }
                return true;
            case R.live_play_closed_v3_name_view:
                ctx.set(`name`, text)
                return true;
            case R.live_square_side_bar_item_title_view:
            case R.live_feed_item_cover_new_style_title_view:
            case R.live_play_closed_v3_title_text_view:
                ctx.set(currentPage, Pages.liveClose)
                ctx.set(`title`, text)
                return true;
            case R.live_cover_icon_text:
                ctx.set('cover', text)
                return true;
            case R.live_activity_red_packet_gift_reward_count_view:
                const moneysData = ctx.get(moneys) || []
                moneysData.push(parseInt(text.replace('x', '')))
                ctx.set(moneys, moneysData)
                return true;
            case R.live_activity_red_packet_count_text_view:
            case R.red_packet_cover_reward_sub_pack_count:
            case R.icon_desc_text_view:
                ctx.set('total', parseInt(text.replace('x', '')))
                return true;
            case R.red_packet_cover_reward_usual_count:
                if (text) ctx.set('count', parseInt(text))
                return true;
            case R.red_packet_cover_reward_usual_unit:
                ctx.set('unit', text)
                return true;
            case R.live_activity_red_packet_grab_bottom_description_view:
            case R.first_line_text_view:
                if (text) ctx.set('condiction', text)
                return true;
            case R.second_line_text_view:
                if (text) ctx.set('condiction', text)
                return true;
            case R.live_activity_red_packet_user_info_description_view:
            case R.common_red_packet_pop_prepare_reward_desc_view:
            case R.red_packet_pop_prepare_source_desc_view:
            case R.title_text_view:
                const res = text.match(/主播的(.*?)红包/)
                if (res && res.length > 0) {
                    const [, type] = res;
                    ctx.set('type', type)
                }
                else if (/.*?穿云箭/.test(text)) {
                    ctx.set('type', '穿云箭')
                }
                else if (/.*?的快币红包/.test(text)) {
                    ctx.set('type', '观众包')
                }
                else if (/.*?礼物红包/.test(text)) {
                    ctx.set('type', '礼物')
                } else {
                    const condiction = ctx.get<string>(`condiction`)
                    if (/(.*?)锦鲤红包/.test(condiction)) {
                        ctx.set('type', '锦鲤')
                    } else {
                        ctx.set('type', '其他')
                    }
                }
                ctx.set('desc', text)
                return true;
            case R.status_text_view:
                if (!ctx.isTop()) {
                    ctx.set('result', text)
                }
                return true;
            case R.live_name_text:
            case R.user_name_text_view:
            case R.user_name:
            case R.live_multi_chat_user_name:
            case R.live_square_side_bar_item_user_name_view:
            case R.tab_name:
                ctx.set(`username`, text)
                return true;
            case R.progress_text_view:
                ctx.set(`progress`, text)
                return true;
            case R.live_square_side_bar_item_title_text_view:
                ctx.set('title', text)
                return true;
            case R.live_feed_item_cover_new_style_audience_count_view:
            case R.live_square_side_bar_item_count_view:
                const isWan = text.match(/(.*?)万/)
                if (isWan && isWan.length > 0) {
                    const [, wan] = isWan
                    ctx.set('countView', parseFloat(wan) * 10000)
                } else {
                    ctx.set('countView', parseInt(text))
                }
                return true;
            case R.live_square_side_bar_item_audience_count_view:
                ctx.set(`audienceCountView`, text)
                return true;
            case R.live_condition_leee_button_text:
                if (text) {
                    const second = this._parseTimeSecond(text)
                    if (second && second > 0) {
                        ctx.set('second', second)
                    } else {
                        const clickable = ctx.findInParent(ast, it => it.clickable())
                        if (clickable) {
                            ctx.set('join', () => {
                                clickable.click()
                                clickable.recycle()
                            })
                        }
                    }
                }
                return true;
            case R.live_activity_red_packet_grab_button_tip_text_view:
                if (text === '点我继续抢红包') {
                    return true;
                }
                ctx.set('join', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.live_activity_red_packet_opened_reward_list_button:
                ctx.set('toLucky', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.live_follow_text:
                ctx.set('toFollow', () => {
                    ast.click()
                    ast.recycle()
                })
                return true;
            case R.live_audience_count_text:
                if (ast.visibleToUser()) {
                    ctx.set(currentPage, Pages.live)
                    ctx.set('online', parseInt(text))
                }
                return true;
            case R.live_hourly_rank_content_view:
                ctx.set('rank', text)
                return true;
            case R.live_side_bar_pendant_text_view:
                const moreBtn = ctx.findInParent(ast, it => it.clickable())
                if (moreBtn) {
                    ctx.set('toMore', () => {
                        moreBtn.click()
                        moreBtn.recycle()
                    })
                }
                return true;
            case R.live_red_packet_status_text:
                const openRedPacket = ctx.findInParent(ast, it => it.clickable())
                if (openRedPacket) {
                    ctx.set('openRedPacket', () => {
                        openRedPacket.click()
                        openRedPacket.recycle()
                    })
                }
                ctx.set('redPacket', text)
                ctx.set('openTime', ctx.formatData(ctx.toSecondDate(ctx.tosecond(text))))
                return true;
            case R.live_top_right_common_pendant_count_down_refreshing:
                const activityBtn = ctx.findInParent(ast, it => it.clickable())
                if (activityBtn) {
                    ctx.set('openActivity', () => {
                        activityBtn.click()
                        activityBtn.recycle()
                    })
                }
                ctx.set('activity', text)
                return true;
            case R.live_multi_chat_gift_text:
                ctx.set('gift', text)
                return true;
            case R.merchant_rank_flipper_title:
                ctx.set('rank', text)
                return true;
            case R.empty:
                if (text === `关注并退出`) {
                    const canClick = ctx.findInParent(ast, it => it.clickable() && it.className() === `android.view.ViewGroup`)
                    if (canClick) {
                        ctx.set(`followAndExit`, () => {
                            canClick.click()
                            canClick.recycle()
                        })
                    }
                }
                if (text === '立即去看') {
                    const canClick = ctx.findInParent(ast, it => it.clickable() && it.className() === `android.view.ViewGroup`)
                    if (canClick) {
                        ctx.set(`toLook`, () => {
                            canClick.click()
                            canClick.recycle()
                        })
                    }
                }
                if (text === '退出直播间') {
                    const canClick = ctx.findInParent(ast, it => it.clickable() && it.className() === `android.view.ViewGroup`)
                    if (canClick) {
                        ctx.set(`exit`, () => {
                            canClick.click()
                            canClick.recycle()
                        })
                    }
                }
                if (text === '退出') {
                    const canClick = ctx.findInParent(ast, it => it.clickable() && it.className() === `android.view.ViewGroup`)
                    if (canClick) {
                        ctx.set(`exit`, () => {
                            canClick.click()
                            canClick.recycle()
                        })
                    }
                }
                return false;
            default:
                if (sid && !ctx.isTop()) {
                    ctx.set(sid, text)
                }
                return false;
        }
    }
}

export const pageVisitor = new PageVisitor()